﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace AgileDesign.Utilities
{
    public static class ObjectExtensions
    {
        public static Dictionary<string, object> Properties(this object obj)
        {
            return obj.GetType().GetProperties()
                .ToDictionary
                (
                    p => p.Name
                    , p => GetValue(p, obj)
                );
        }

        private static object GetValue(PropertyInfo p, object obj)
        {
            try
            {
                return p.GetValue(obj, null);
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}
